package com.lz.repository.zhy.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lz.entity.Employee;
import com.lz.repository.zhy.EmployeeInfoDao;
@Repository
public class EmployeeInfoDaoImpl implements EmployeeInfoDao  {
     
	// 通过@Autowired注解注入Spring容器中的SessionFactory实例
		@Autowired
		private SessionFactory sessionFactory;
	
	@Override
	public List<Employee> search(Employee cond) {
		/*
         * 使用HQL查询，优点：
         * 1. 直接针对实体类和属性进行查询，不用写繁琐的SQL语句
         * 2. 查询结果直接保存在List对象中，不用再次封装
         * 3. 可以通过配置dialect属性，对不同数据库自动生成不同的用于执行的SQL语句。
         * 
         */
		// 获得session
			Session session = sessionFactory.openSession();
			String hql = "from employee as el where el.employeeName=:employeeName and el.password=:password";
			Query query = session.createQuery(hql);
			query.setString("employeeName", cond.getEmployeeName());
			query.setString("password", cond.getPassword());
			List<Employee> list = query.list();
		return list;
	}
  
}
